package com.group.controller;

import com.group.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import pojo.entity.Address;
import pojo.qo.AddressState;
import utils.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

@Api(tags = "收货地址")
@Controller
@RequestMapping(value = "/address" , produces = "text/html;charset=UTF-8")
public class AddressController extends BaseController {

    @Autowired
    private AddressService addressService;

    @Autowired
    RestTemplate restTemplate;

    //增加
    @ApiOperation("添加地址")
    @RequestMapping(value = "/addAddress.do")
    @ResponseBody
    public String addAddress(Address address, HttpServletResponse response){
        int i = addressService.addAddress(address);
        System.out.println("addressController.addAddress："+i);
        String s = writerSuccessResult(i, response);
        return s;
    }

    //删除
    @ApiOperation("删除地址")
    @RequestMapping(value = "/deleteByAddressId.do")
    @ResponseBody
    public String deleteByAddressId (Long addressId, HttpServletResponse response){
        System.out.println("删除地址.addressId："+addressId);
        int i = addressService.deleteByAddressId(addressId);
        String s = writerSuccessResult(i, response);
        return s;
    }

    //根据用户id查询此用户的所有地址
    @ApiOperation("查询该用户的所有地址")
    @RequestMapping(value = "/queryByUserIdGetAllAddress.do")
    @ResponseBody
    public String queryByUserIdGetAllAddress( Long userId, HttpServletResponse response){
        System.out.println("addressController.所有地址："+userId);
        List<Address> addresses = addressService.queryByUserIdGetAllAddress(userId);
        System.out.println("addressController.所有地址："+addresses);
        try{
            String s = writerSuccessResult(addresses, response);
            System.out.println("addressController.所有地址："+s);
            return s;
        }catch (Exception e){
            String errorMsg = e.getMessage();
            String s = writerErrorResult(errorMsg, response);
            System.out.println("addressController.所有地址："+s);
            return s;
        }

    }

    //根据用户id查询此用户的默认地址
    @ApiOperation("查询用户默认地址")
    @RequestMapping(value = "/queryByUserIdForDefault.do")
    @ResponseBody
    public String queryByUserIdForDefault(Long userId, HttpServletResponse response){
        System.out.println("查询用户默认地址.userId:"+userId);
        Address address = addressService.queryByUserIdForDefault(userId);
        System.out.println("查询用户默认地址.默认地址:"+address);
        String s = writerSuccessResult(address, response);
        return s;
    }

    //根据地址id查询地址信息
    @ApiOperation("查询该地址信息")
    @RequestMapping(value = "/qqqqueryByAddressId.do")
    @ResponseBody
    public String queryByAddressId(Long addressId, HttpServletResponse response){
        Address address = addressService.queryByAddressId(addressId);
        String s = writerSuccessResult(address,response);
        return s;
    }

    //设为当前地址为默认地址且当前useid的其他地址为非默认地址
    @ApiOperation("设置默认地址")
    @RequestMapping(value = "/addressToDefault.do")
    @ResponseBody
    public String addressToDefault(@RequestBody AddressState address, HttpServletResponse response){
        System.out.println("设为默认:"+address);
        addressService.addressToDefault(address);
        /*AddressController addressController = new AddressController();
        addressController.queryByUserIdForDefault(addressState.getUserId(), response);*/
        return writerSuccessResult("",response);
    }

    @ApiOperation("获取ip地址")
    @RequestMapping(value="/getAddressIp.do")
    @ResponseBody
    public String getAddressIp(HttpServletRequest request) throws URISyntaxException {

        String ak = "6ld7i8Eya53ogaRYkx90LshtQdr9n7jq";

        String ip = request.getRemoteHost();
        ip = "61.144.96.217";
        System.out.println("获取ip"+ip);
        //Http协议
        String url = "http://api.map.baidu.com/location/ip?ak="+ak+"&ip="+ip+"&coor=bd09ll ";
        //http请求
        ResponseEntity<Map> fe = restTemplate.getForEntity(url, Map.class);
        Map body = fe.getBody();
        String s = writerSuccessResult(body);
        return s;
    }

}
